package com.pb.wkflow.api.query;

import com.pb.infra.oth.util.SpringContextUtil;
import com.pb.wkflow.api.entity.CompleteTaskEntity;
import com.pb.wkflow.api.entity.TaskCountEntity;
import com.pb.wkflow.api.mapper.CompleteTaskMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.camunda.bpm.engine.impl.AbstractQuery;
import org.camunda.bpm.engine.impl.Page;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;

import java.util.List;

/**
 * @author 苗益辉
 * @date 2022/6/20 16:29
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class CompleteTaskQuery  extends AbstractQuery<CompleteTaskQuery, CompleteTaskEntity> {

    /**
     * 流程实例id
     */
    private String processInstanceId;
    /**
     * 流程定义id
     */
    private String processDefineId;
    /**
     * 流程定义键值
     */
    private String procDefineKey;
    /**
     * 流程定义键值模糊查询
     */
    private String procDefineKeyLike;
    /**
     * 流程定义名称
     */
    private String procDefineName;
    /**
     * 流程定义名称模糊查询
     */
    private String procDefineNameLike;
    /**
     * 任务定义键值
     */
    private String taskDefineKey;
    /**
     * 任务定义键值模糊查询
     */
    private String taskDefineKeyLike;
    /**
     * 任务id
     */
    private String taskId;
    /**
     * 任务名称
     */
    private String taskName;
    /**
     * 任务名称模糊查询
     */
    private String taskNameLike;
    /**
     * 租户id
     */
    private String tenantId;
    /**
     * 处理人
     */
    private String userId;
    /**
     * 处理人列表
     */
    private List<String> userIds;
    /**
     * 流程开始时间
     */
    private String processCreateTimeStart;
    /**
     * 流程结束时间
     */
    private String processCreateTimeEnd;
    /**
     * 任务接收时间开始
     */
    private String startTimeStart;
    /**
     * 任务接收时间结束
     */
    private String startTimeEnd;
    /**
     * 任务完成时间开始
     */
    private String endTimeStart;
    /**
     * 任务完成时间结束
     */
    private String endTimeEnd;
    /**
     * 接收时间排序
     */
    private String startTimeSort;
    /**
     * 完成时间排序
     */
    private String endTimeSort;
    /**
     * 申请用户名称模糊查询
     */
    private String startUserNameLike;
    /**
     * 申请机构名称模糊查询
     */
    private String startOrgNameLike;

    /**
     * 查询已办列表服务
     */
    private final CompleteTaskMapper completeTaskMapper = SpringContextUtil.getBean(CompleteTaskMapper.class);

    public void startTimeAsc() {
        this.startTimeSort = "asc";
    }

    public void startTimeDesc() {
        this.startTimeSort = "desc";
    }

    public void endTimeAsc() {
        this.endTimeSort = "asc";
    }

    public void endTimeDesc() {
        this.endTimeSort = "desc";
    }

    @Override
    public long executeCount(CommandContext commandContext) {
        return completeTaskMapper.findCompleteListCount(this);
    }

    @Override
    public List<CompleteTaskEntity> executeList(CommandContext commandContext, Page page) {
        return completeTaskMapper.findCompleteList(this);
    }

    /**
     * 查询各流程类型待办数量
     * @return List<TaskCountEntity>
     */
    public List<TaskCountEntity> countProcDefKey() {
        return completeTaskMapper.countProcDefKey(this);
    }
}
